package tools;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.testng.Assert.assertEquals;

public class UseBrowser_Remote {


    public WebDriver wd;


    @Test
    public WebDriver setupChromeRemote() throws MalformedURLException {

        DesiredCapabilities desiredCapabilities;
//        判断要打开的浏览器


        ChromeOptions chromeOptions = new ChromeOptions();
       //  chromeOptions.addArguments("--headless");
       // chromeOptions.addArguments("--disable-gpu");
       // chromeOptions.addArguments("--window-size=1920,1080");

        desiredCapabilities = DesiredCapabilities.chrome();
        desiredCapabilities.setCapability("build", "test123");
        desiredCapabilities.setCapability("recordVideo", true);
        desiredCapabilities.setCapability("chromeOptions", chromeOptions);

//        拼接处要执行脚本的node 节点地址
        // String url = nodeURL + "/wd/hub";
        String url = "http://192.168.1.17:4444/wd/hub";
        // String url = "http://192.168.1.13:9222";
        // String url = "http://192.168.1.13:9515";
        wd = new RemoteWebDriver(new URL(url), desiredCapabilities);

       wd.manage().window().maximize();
        return wd;
    }


    @Test
    public WebDriver setupFirefoxRemote() throws MalformedURLException {

        DesiredCapabilities desiredCapabilities;
//        判断要打开的浏览器


        FirefoxOptions firefoxOptions = new FirefoxOptions();

        //firefoxOptions.addArguments("--headless", "--disable-gpu", "--window-size=1290,1080");
      //  firefoxOptions.addArguments( "--disable-gpu", "--window-size=1290,1080");
        firefoxOptions.addArguments( "--disable-gpu");


        desiredCapabilities = DesiredCapabilities.firefox();
        desiredCapabilities.setCapability("build", "osc_login_firefox");
        desiredCapabilities.setCapability("recordVideo", true);

        desiredCapabilities.setCapability("firefoxOptions", firefoxOptions);

//        拼接处要执行脚本的node 节点地址
        // String url = nodeURL + "/wd/hub";
        String url = "http://192.168.1.17:4444/wd/hub";
        wd = new RemoteWebDriver(new URL(url), desiredCapabilities);
        wd.manage().window().maximize();
        return wd;
    }




    public void PlaySpeed(){
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



    public void test_wd() {
        try {
            Thread.sleep(3600);
            wd.close();
            wd.quit();
            System.out.println("----close browser!!!-----");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("wd ---ok");
    }

    /**
     * 是否存在某个节点
     *
     * @param by
     * @param wd
     * @return
     */
    public boolean isPresentEle(By by, WebDriver wd) {
        try {
            wd.findElement(by);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 获取当前时间，并转换为字符串
     * @return
     */
    private static String Date2String(){
        Format format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date());
    }

    /**
     * 自动截屏
     * @param wd
     */
    public  void takeScreenshot(WebDriver wd){
        try {
            //获取截图file
            File srcFile= ((TakesScreenshot)wd).getScreenshotAs(OutputType.FILE);


            //新文件
            File targeFile = new File("pic/" + Date2String()+".png");

            //将图片移动到指定位置
            FileUtils.copyFile(srcFile, targeFile);
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取断言失败的原因、信息和截屏
     * @param one
     * @param two
     * @param wd
     * @throws IOException
     * @throws InterruptedException
     */
    public  void assertEqu(String one,String two,WebDriver wd) throws InterruptedException, IOException{
        try{
            Thread.sleep(3000);
            takeScreenshot(wd);
            assertEquals(one, two);

        }catch(Exception e){
            System.out.println(e.getCause());
            System.out.println(e.getMessage());
        }
    }

}






